<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 角色模型
 * 
 * @desc    角色相关逻辑
 * @version 1.0.0 
 * @author  Jason
 * @date    2017年4月28日 下午3:03:35
 */
class Mod_role extends MY_Model
{
	protected $table_mapper = [
			'master' => 'role',
			'action' => 'role_action',
			'acl'    => 'role_acl',
	];
	
	protected $allowed_fields = [
			'master' => ['name', 'description', 'status'],
			'action' => ['code', 'name', 'group_code', 'group_name', 'description'],
			'acl'    => ['role_id', 'action_id'],
	];
	
	use models\common\BinaryBitBasedStatus;
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->database();
		
		$this->status_bit_config = [
				'master' => [
						// 状态：启用禁用
						'status_bit_length' => 1,
						'status_bit_positive_tags' => ['enabled'],
						'status_bit_negative_tags' => ['disabled'],
				],
				'action' => [
						// 无
				],
				'acl' => [
						// 无
				],
		];
	}
	
	
	/**
	 * 插入前处理器
	 *
	 * @desc  主要负责格式化一些字段
	 * @param array  $data
	 * @param string $mapping	表映射
	 *
	 * @return array
	 */
	public function before_create_data_handler($data, $mapping = 'master')
	{
		if ($mapping == 'master') {
			if (empty($data['status_tags'])) {
				$data['status_tags'] = [];
			} else if (!is_array($data['status_tags'])) {
				$data['status_tags'] = explode(',', $data['status_tags']);
			}
			$data['status'] = $this->calculate_status_by_tags($data['status_tags']);
		}
		
		return $data;
	}
	
	/**
	 * 更新前处理器
	 *
	 * @desc  主要负责格式化一些字段 & 过滤掉多语言字段，确保只在默认语言下更改主表的多语言字段
	 * @param number $id
	 * @param array  $data
	 * @param string $mapping	表映射
	 *
	 * @return array
	 */
	public function before_update_data_handler($id, $data, $mapping = 'master')
	{
		$data = $this->before_create_data_handler($data, $mapping);
		
		return $data;
	}
	
	public function get_all($mapping = 'master')
	{
		$this->db->order_by('id', 'ASC');
		$result = $this->db->get($this->table_mapper[$mapping]);
		return $result->result_array();
	}
	
	public function get_role_actions($role_id)
	{
		$this->db->from($this->table_mapper['acl']);
		$acls = $this->db->where("role_id=$role_id")->get()->result_array();
		
		$this->db->from($this->table_mapper['action']);
		$ids = array_column($acls, 'action_id');
		$ids[] = -1;
		$actions = $this->db->where_in('id', $ids)->get()->result_array();
		$actions = array_column($actions, null, 'id');
		
		foreach ($acls as $acl) {
			$actions[$acl['action_id']]['acl_id'] = $acl['id'];
		}
		
		return $actions;
	}
	
	public function set_role_actions($role_id, $action_ids)
	{
		$this->clear_role_actions($role_id);
		
		if(is_string($action_ids)) {
			$action_ids = array_filter(explode(',', $action_ids), 'strlen');
		}
		if(empty($action_ids)) {
			return true;
		}
		foreach($action_ids as $action_id)
		{
			$data = array(
					'role_id' => $role_id,
					'action_id' => $action_id,
			);
			$this->db->insert($this->table_mapper['acl'], $data);
		}
		return true;
	}
	
	public function clear_role_actions($role_id)
	{
		$this->db->where('role_id', $role_id);
		$this->db->delete($this->table_mapper['acl']);
	}
	
	
}